package com.android.fastride.model.http.entity;

/**
 * 一个统一的包容的bean
 * Created by pengyuantao on 2017/9/21 14:11.
 */

public class RxBean {

  //缓存的最大个数
  private static final int MAX_CACHE = 20;

  //缓存链表的头部
  private static RxBean sHeader;

  //线程同步锁
  private static final Object sLock = new Object();

  //当前缓存池的大小
  private static int sCurCacheSize;

  //链表的下一个元素
  private RxBean next;

  //int类型数据
  private int intValue;

  //float类型数据
  private float floatValue;

  //long类型数据
  private long longValue;

  //string类型数据
  private String stringValue;

  //obj类型数据
  private Object objectValue;

  //获取一个RxBean对象
  public static  RxBean  obtain(){
    synchronized (sLock) {
      if (sHeader != null) {
        RxBean rxBean = sHeader;
        sHeader = rxBean.next;
        rxBean.next = null;
        sCurCacheSize--;
      }
    }
    return new RxBean();
  }

  /**
   * 对数据进行赋值
   * @param intValue
   * @param floatValue
   * @param longValue
   * @param stringValue
   */
  public RxBean setValue(int intValue, float floatValue, long longValue, String stringValue) {
    this.intValue = intValue;
    this.floatValue = floatValue;
    this.longValue = longValue;
    this.stringValue = stringValue;
    return this;
  }

  /**
   * 对int类型和String类型进行赋值
   * @param intValue
   * @param stringValue
   */
  public RxBean setValue(int intValue, String stringValue) {
    this.intValue = intValue;
    this.stringValue = stringValue;
    return this;
  }

  /**
   * 设置Object类型数据
   * @param objectValue
   */
  public RxBean setObjectValue(Object objectValue) {
    this.objectValue = objectValue;
    return this;
  }

  /**
   * 回收数据
   */
  public void recycle(){
    intValue = 0;
    floatValue = 0f;
    longValue = 0;
    stringValue = null;
    objectValue = null;
    synchronized (sLock) {
      if (sCurCacheSize < MAX_CACHE) {
        next = sHeader;
        sHeader = this;
        sCurCacheSize++;
      }
    }
  }

  public int getIntValue() {
    return intValue;
  }

  public float getFloatValue() {
    return floatValue;
  }

  public long getLongValue() {
    return longValue;
  }

  public String getStringValue() {
    return stringValue;
  }

  public Object getObjectValue() {
    return objectValue;
  }

  @Override
  public String toString() {
    return "RxBean{"
        + "next="
        + next
        + ", intValue="
        + intValue
        + ", floatValue="
        + floatValue
        + ", longValue="
        + longValue
        + ", stringValue='"
        + stringValue
        + '\''
        + ", objectValue="
        + objectValue
        + '}';
  }
}
